﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GameEngine.View.Components;
using GameEngine.Model;
using System.Collections.Generic;
using PureMVC.Patterns;
using PureMVC.Interfaces;

namespace GameEngine.View
{
    public class GameMediatorHelper<MyIdType>
        where MyIdType : IComparable<MyIdType>
    {
        public static string GetMediatorNameFromProxy(IGameDataProxy<MyIdType> proxy)
        {
            return proxy.VO.Id.Value + "_GameMediator";
        }
    }

    public class GameMediator<MyIdType, MyBaseUIElementType> : Mediator
        where MyIdType : IComparable<MyIdType>
    {
        //public const string ADDITEMTOMAP_NOTIFICATION = "MapMediator_AddItem";
        //public const string REMOVEITEMFROMMAP_NOTIFICATION = "MapMediator_RemoveItem";
        //public const string MOUSEONITEMS_NOTIFICATION = "MapMediator_RemoveItem";

        public readonly IGameDataProxy<MyIdType> proxy;
        //public readonly LevelMediator<MyLevelView, MyWorldView, MyMapView, MyItemView, MyIdType, MyBaseUIElementType> levelMediator;

        public GameMediator(
            IGameDataProxy<MyIdType> dataProxy,
            //LevelMediator<MyLevelView, MyWorldView, MyMapView, MyItemView, MyIdType, MyBaseUIElementType> levelMediator,
            IGameView<MyIdType, MyBaseUIElementType> view)
            : base(GameMediatorHelper<MyIdType>.GetMediatorNameFromProxy(dataProxy), view)
        {
            this.proxy = dataProxy;
            //this.levelMediator = levelMediator;

            //foreach (IThingDataProxy<MyIdType> itemProxy in proxy.Items.Values)
            //{
            //    ViewComponent.AddUIElement(
            //        itemMediator.GetThingDataProxyView(itemProxy),
            //        itemProxy.Position,
            //        proxy.Map.GetTileHeight(itemProxy.Position).Value);
            //}

            
       }

        public new IGameView<MyIdType, MyBaseUIElementType> ViewComponent
        {
            get { return (IGameView<MyIdType, MyBaseUIElementType>)base.ViewComponent; }
        }

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            IList<String> notifications = base.ListNotificationInterests();
            
            notifications.Add(proxy.LEVELCHANGE_NOTIFICATION);

            return notifications;
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                default:
                    if (notification.Name.Equals(proxy.LEVELCHANGE_NOTIFICATION)) {
                        var lvlMediator = ViewFacade<MyIdType, MyBaseUIElementType>.Helper.
                            GetLevelMediator(proxy.CurrentLevel.VO.Id);
                        ViewComponent.SetLevelView(lvlMediator.ViewComponent);
                    }
                    break;
                    
            }
            base.HandleNotification(notification);
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            //ViewComponent.SetLevelView(levelMediator.ViewComponent);
            //ViewComponent.SetMapView(mapMediator.ViewComponent);
            //add dos items existentes?
            //itemMenuMediator.
            //itemMediator.OnPositionChange += new Action<IThingDataProxy<MyIdType>>(itemMediator_OnPositionChange);
            //ViewComponent.MyOnSizeChanged += new Action<Point>(ViewComponent_OnSizeChanged);
            //itemMediator.OnDirectionChange += new Action<IThingDataProxy<ItemVOId>>(itemMediator_OnDirectionChange);
        }

        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
            //Facade.RemoveMediator(mapMediator.MediatorName);
            //Facade.RemoveMediator(Menu.MediatorName);
           // Facade.RemoveMediator(MenuBar.MediatorName);
        }

        #endregion
    }
}
